<?php
session_start();
header('Content-type:text/html; charset=UTF-8');
/**
* @copyright www.cndns.com
* @date 2010-04-09
* @author Jane
*/

include_once('config.php');

$err = '&nbsp;';
$logn = (isset($_SESSION['login']) && ($_SESSION['login'] == 'ok')) ? true : false;
// 登录验证
if ( isset($_POST['login']) & !empty($_POST['login']) ) 
{
	//$user = $_POST['user'];
	$upwd = $_POST['upwd'];
	// admin/QDBVhR8CLF
	if ( ($upwd) <> '3e4a1255f3326396f3a97b5fbd2eb069' ) {
		$err = '密码错误！';
	} else {
		$logn = true;
		$_SESSION['login'] = 'ok';
	}	
}

// 省份列表
$prvlist = array('-请选择-','安徽','北京','重庆','福建','甘肃','广东','广西','贵州','海南'
                ,'河北','黑龙江','河南','香港','湖北','湖南','江苏','江西','吉林','辽宁'
				,'澳门','内蒙古','宁夏','青海','山东','上海','山西','陕西','四川','台湾'
				,'天津','新疆','西藏','云南','浙江');

// 正常登录
if ( $logn ) {
	// mysql
	$link = mysql_connect(Config::$db_host, Config::$db_user, Config::$db_pass) or die(ERROR);
	mysql_query('SET NAMES utf8', $link);
	mysql_select_db(Config::$db_name);

	// 分页初始化设定
	$perpage = 10;//每页显示结果集行数
	$pagenum = 9;// 1 | 2 | 3 | 4 | 5 ...

	// 检索条件设定
	$where = "1";
	if ( isset($_POST['send']) || isset($_GET['page']) ) {
		$prv = isset($_POST['prv']) ? $_POST['prv'] : $_SESSION['search']['prv'];
		$sdate = isset($_POST['sdate']) ? $_POST['sdate'] : $_SESSION['search']['sdate'];
		$edate = isset($_POST['edate']) ? $_POST['edate'] : $_SESSION['search']['edate'];
		
		if ( !empty($prv) ) {
			$_SESSION['search']['prv'] = $prv;
			$where .= " AND `ip_prv` LIKE '{$prv}%'";
		}else{
			unset($_SESSION['search']['prv']);
		}

		if ( !empty($sdate) ) {
			$_SESSION['search']['sdate'] = $sdate;
			$sdate = strtotime( $sdate.' 00:00' );
			$where .= " AND `create_time` >= '{$sdate}'";
		}else{
			unset($_SESSION['search']['sdate']);
		}

		if ( !empty($edate) ) {
			$_SESSION['search']['edate'] = $edate;
			$edate = strtotime( $edate.' 23:59' );
			$where .= " AND `create_time` <= '{$edate}'";
		}else{
			unset($_SESSION['search']['edate']);
		}
	} else {
		$_SESSION['search'] = array();
	}

	// sql
	$sql = "SELECT count(*) FROM `feedback` WHERE {$where}";
	//echo $sql."<br>";
	$result = mysql_query( $sql, $link );
	// 记录集行数
	$rs = mysql_fetch_array($result);
	$total = $rs[0];
	$totalPage = ceil( $total/$perpage ); // 总页数
	// 分页
	$paging = pages( 'count.php', $total, $perpage, $pagenum );

	$page = empty( $_GET['page'] ) ? 1 : $_GET['page'];
	if ( !is_numeric($page) ) exit('request failed');
	$page = $totalPage ? min( $page, $totalPage ) : $page;
	$start = ( $page - 1 )*$perpage;

	//统计一览
	$sql = "SELECT * FROM `feedback` WHERE {$where} ORDER BY `create_time` DESC LIMIT {$start}, {$perpage}"; 
	$result2 = mysql_query( $sql, $link );
	while( $row = mysql_fetch_assoc( $result2 ) )
	{
		$list[] = $row;
	}
}

// 分页函数
function pages( $pageUrl, $total, $perpage = 20, $pagenums = 10, $showOne = true )
{
	$page = intval( $_GET['page'] );

	$totalPage = ceil( $total/$perpage );
	$totalPage = max( $totalPage, 1 );
	$pagenums = min( $pagenums, $totalPage );

	$page = max( $page, 1 );
	$page = min( $page, $totalPage );


	$sqlStart = ( $page - 1 )* $perpage;

	if( $showOne ) {
		$pageStart = min( max( 1, $totalPage - $pagenums ), max( 1, $page - ceil( $pagenums/2 ) ) );
		$pageEnd = min( $pageStart+$pagenums, $totalPage );

		for( $i = $pageStart; $i <= $pageEnd; $i++ ) {
			$pageArray['page'] = $i;
			$pageArray['link'] = $pageUrl.'?page='.$i;

			$pages[] = $pageArray;
		}
	} else {
		$sqlStart = 0;
		$pages = array();
	}

	return array( $sqlStart, $perpage, $totalPage, $pages );
}

// IP归属地
function ipowner( $ip )
{
	return file_get_contents('http://licence.sitestar.cn/ipdata/ip_location_utf8.pl?ip='.$ip);
}

// IP转码函数
function ipencode( $ip )
{
	$tmp = explode( '.', $ip );
	$str = $tmp[0]*pow(256, 3) + $tmp[1]*pow(256, 2) + $tmp[2]*256 + $tmp[3];
	return $str;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>SiteStar - 建站之星</title>
	<style type="text/css">
	* {font-size:12px;}
	#main {margin:0 auto;text-align:center;}
	#main span, table {display:inline-block;}
	#main span {font-size:14px;font-weight:bold;width:100%;}
	#main label {display:block;color:#FF0000;font-size:14px;padding-bottom:5px;}
	#showlist {border-left:1px solid #C8C8C8;border-top:1px solid #C8C8C8;display:inline-block;}
	#showlist th,td {border-bottom:1px solid #C8C8C8;border-right:1px solid #C8C8C8;text-align:center;}
	#lgntbl td,#search td {border:0;}
	#search {margin-top:10px;}
	#paging {margin-top:5px;}
	.up {background-color:#C8C8C8;cursor:pointer;}
    .off {background-color:#FFF;}
    .putstyle {width:180px;}
	</style>
	<script language="javascript">
	<!--
	function logn_check() {
//		var user = document.lgnform.user;
		var upwd = document.lgnform.upwd;
//		if ( !trim(user.value) ) {
//			alert('请输入用户名');
//			user.focus();
//			return false;
//		}
		
		if ( !trim(upwd.value) ) {
			alert('请输入密码');
			upwd.focus();
			return false;
		}
	}
	function trim( str ) { 
		return str.replace(/(^\s*)|(\s*$)/g, '');
	}
	//-->
	</script>
	<script language="javascript" type="text/javascript" src="./datepicker/WdatePicker.js"></script>
</head>

<body>

<div id="main">
<?php
	if ( $logn ) {
?>
	<span>"建站之星"软件安装统计一览</span>
	<form name="sfrm" method="post" action="">
	<table cellpadding="5" cellspacing="0" border="0" id="search">
		<tr><td>所属省份：</td><td><select style="width:100px;float:left;" name="prv"><?php
			foreach( $prvlist as $value ) 
			{
				if ( $value == '-请选择-' ) { 
					echo '<option value="">'.$value.'</option>';
				} else {
					$selected = ($_SESSION['search']['prv'] == $value) ? 'selected=true' : '';
					echo '<option value="'.$value.'" '.$selected.'>'.$value.'</option>';
				}
			}
	    ?></select></td></tr>
		<tr><td>安装日期：</td><td><input type="text" class="Wdate" name="sdate" id="sdate" value="<?=$_SESSION['search']['sdate'];?>" onfocus="WdatePicker({isShowClear:true,maxDate:'#F{$dp.$D(\'edate\');}'})" readonly />
		 ~ <input type="text" class="Wdate" name="edate" id="edate" value="<?=$_SESSION['search']['edate'];?>" onfocus="WdatePicker({isShowClear:true,minDate:'#F{$dp.$D(\'sdate\');}'})" readonly />
		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="send" value=" 查 询 " /></td></tr>
    </table></form>
	<label>安装总次数：<b><?=$total;?></b></label>
	<table cellpadding="5" cellspacing="0" border="0" id="showlist">
		<thead>
		<tr>
		    <th>IP</th>
			<th>归属地</th>
			<th>域名</th>
		    <th>操作系统</th>
		    <th>软件版本</th>
	   		<th>模板</th>
		    <th>PHP版本</th>
		    <th>MySQL版本</th>
		    <th>安装日期</th>
	    </tr>
		</thead>
		<tbody>
		<?php
	    	$ln = sizeof( $list );
            if ( $ln ) {
				for( $i=0; $i<$ln; $i++ ) {
	    ?>
	    <tr style="cursor:pointer;background:#FFF;" onMouseOver="this.style.background='lightblue'" onMouseOut="this.style.background='#FFF'">
		    <td><?=$list[$i]['ip'];?></td>
			<td><?php
			$wanip = substr($list[$i]['ip'], 0, strpos($list[$i]['ip'], '/'));
		    echo $list[$i]['ip_prv'].'<br />'.ipowner($wanip);?></td>
			<td><A HREF="http://<?=$list[$i]['domain'];?>" target="_blank"><?=$list[$i]['domain'];?></A></td>
		    <td><?=$list[$i]['vos'];?></td>
		    <td><?=$list[$i]['version'];?></td>
		    <td><?=$list[$i]['tpl_name'];?></td>
		    <td><?=$list[$i]['vphp'];?></td>
			<td><?=$list[$i]['vmysql'];?></td>
			<td><?=date('Y-m-d H:i', $list[$i]['create_time']);?></td>
		</tr>
		<?php
				}
			} else {
	    ?>
	    <tr>
	    	<td colspan="9">暂无记录</td>	
	    </tr>
	    <?php } ?>
		</tbody>
	</table>
	<?php if($ln){ ?>
	<div id="paging">
		当前页/共计&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?=$page;?>/<?=$totalPage;?><br />
		<a href="count.php?page=1">首页</a> | 
		<?php
			$links = $paging[3];
		    $cnt = sizeof( $links );
		    for( $k=0; $k<$cnt; $k++ ) {
		?>
		<a href="<?=$links[$k]['link'];?>"><?=$links[$k]['page'];?></a> | 
		<?php } ?>
		<a href="count.php?page=<?=$paging[2];?>">末页</a>
	</div>
<?php 
		}
} else {
?>
	<form name="lgnform" method="post" action="" onsubmit="return logn_check();">
	<table cellpadding="5" cellspacing="0" border="0" id="lgntbl">
		<!--tr>
			<td>用户名：</td>
	   		<td><input type="text" name="user" value="<?=$user;?>" class="putstyle" /></td>
	    </tr-->
	    <tr>
			<td>请输入查看密码：</td>
	   		<td><input type="password" name="upwd" value="<?=$upwd;?>" class="putstyle" /></td>
	    </tr>
		<tr><td colspan="2" style="color:#FF0000"><?=$err;?></td></tr>
	    <tr>
			<td colspan="2"><input type="submit" name="login" value=" 查 看 " /></td>
		</tr>
	</table>
	</form>
<?php } ?>
</div>
</body>
</html>
